package com.rdzn.mall.portal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.rdzn.mall.center.enums.CouponHistoryUseStatus;
import com.rdzn.mall.base.common.ResultCode;
import com.rdzn.mall.center.entity.SmsCoupon;
import com.rdzn.mall.center.entity.SmsCouponHistory;
import com.rdzn.mall.center.enums.CouponType;
import com.rdzn.mall.center.service.ISmsCouponHistoryService;
import com.rdzn.mall.center.service.ISmsCouponService;
import com.rdzn.mall.portal.domain.Coupon;
import com.rdzn.mall.portal.domain.CouponHistory;
import com.rdzn.mall.base.exception.Asserts;
import com.rdzn.mall.portal.service.ICouponService;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: sms_coupon
 * @Author: jeecg-boot
 * @Date:   2020-08-27
 * @Version: V1.0
 */
@Service
public class CouponServiceImpl implements ICouponService {

    @Autowired
    private ISmsCouponService couponService;
    @Autowired
    private ISmsCouponHistoryService couponHistoryService;

    @Override
    public List<SmsCoupon> listByProductId(Integer productId) {
        return null;
    }

    @Override
    public SmsCouponHistory exchange(Integer memberId, String code) {
        SmsCoupon coupon = couponService.getByCode(code);
        if (coupon == null){
            Asserts.fail(ResultCode.VALIDATE_FAILED);
        }
        if (coupon.getCount() <= coupon.getReceiveCount()){
            Asserts.fail("亲~优惠券被抢空，下次快点哦~");
        }
        List<SmsCouponHistory> historyList = couponHistoryService.listByCouponIdAndMemberId(coupon.getId(), memberId);
//        if (CollectionUtils.isEmpty(historyList)){
//            Asserts.fail("优惠券信息无效");
//        }
        if (coupon.getPerLimit() <= historyList.size()){
            Asserts.fail("亲~您的领取数量已达上限");
        }
        if (DateUtils.dateDiff('d', coupon.getEndTime(), new Date()) < 0){
            Asserts.fail("亲~优惠券已过期");
        }
        SmsCouponHistory history = new SmsCouponHistory();
        history.setMemberId(memberId);
        history.setCouponCode(code);
        history.setCouponId(coupon.getId());
        history.setGetType(1);
        history.setUseStatus(0);
        history.setCreateTime(new Date());
        couponHistoryService.save(history);
        return history;
    }

    @Override
    public IPage<CouponHistory> page(IPage<SmsCouponHistory> historyIPage) {
        if (historyIPage == null || historyIPage.getSize() <= 0){
            return null;
        }
        return historyIPage.convert(smsCouponHistory -> {
            CouponHistory history = new CouponHistory();
            BeanUtils.copyProperties(smsCouponHistory, history);
            SmsCoupon coupon = couponService.getById(history.getCouponId());
            if (coupon == null || (history.getUseStatus() == 0 && coupon.getEndTime().before(new Date()))){
                smsCouponHistory.setUseStatus(2);
                couponHistoryService.updateById(smsCouponHistory);
            }else {
                Coupon c = new Coupon();
                BeanUtils.copyProperties(coupon, c);
                c.setTypeValue(CouponType.get(c.getType()).VALUE);
                history.setCoupon(c);
            }
            return history;
        });
    }

    @Override
    public List<SmsCouponHistory> listByMemberId(Integer memberId) {
        return couponHistoryService.listByMemberIdAndUseStatus(memberId, CouponHistoryUseStatus.VALID.CODE);
    }

    @Override
    public int countByMemberId(Integer memberId) {
        List<SmsCouponHistory> historyList = couponHistoryService.listByMemberIdAndUseStatus(memberId, CouponHistoryUseStatus.VALID.CODE);
        if (CollectionUtils.isNotEmpty(historyList)){
            return historyList.size();
        }
        return 0;
    }
}
